#include "debug.h"
#include "WStringSupport.h"
#include "AppDelegate.h"

Debug::Debug() :
	debugMsg(""),
	labelMsg(nullptr),
	deltaTime(0),
	mousePosi(Point(0, 0)),
	mouseButton(0)
{
	ttfStyle = new TTFConfig();
	ttfStyle->fontFilePath = FONTB;
	ttfStyle->fontSize = 20;
	ttfStyle->distanceFieldEnabled = false;
	ttfStyle->glyphs = GlyphCollection::DYNAMIC;
	ttfStyle->customGlyphs = nullptr;
}

Debug::~Debug()
{
	safeRelease(ttfStyle);
	safeRelease(debugLayer);
}

Label* Debug::debugLabelMsg(const char * msg)
{
	labelMsg = Label::createWithTTF(*ttfStyle, A2U(msg), TextHAlignment::LEFT);
	labelMsg->enableOutline(Color4B(0, 0, 0, 255), 2);
	labelMsg->setAnchorPoint(Vec2(1, 0));
	labelMsg->ignoreAnchorPointForPosition(false);
	labelMsg->setPosition(Vec2(frameSize.width, 150));
	labelMsg->setGlobalZOrder(99.f);
	return labelMsg;
}

void Debug::showMsg(Node * target, string msg)
{
	auto pLabelDebug = target->getChildByName("labelDebugMsg");
	if (pLabelDebug == nullptr)
	{
		labelMsg = Label::createWithTTF(*ttfStyle, A2U(msg), TextHAlignment::LEFT);
		labelMsg->enableOutline(Color4B(0, 0, 0, 192), 1);
		UI::setNodeAnch(labelMsg, ANCH_BL);
		labelMsg->setPosition(Point(0, 75));
		labelMsg->setGlobalZOrder(99.f);
		labelMsg->setName("labelDebugMsg");
		target->addChild(labelMsg);
	}
	else
	{
		labelMsg->setString(msg);
		//labelMsg->updateContent();
	}
}

void Debug::safeRelease(void* obj)
{
	free(obj);
	obj = nullptr;
}
